<?php

namespace app\controller;


use app\BaseController;

use Aws\S3\S3Client;
use Aws\Exception\AwsException;

class Index extends BaseController
{

    public function index()
    {
        if(request()->isPost() && input('url')){
            $s3Client = new S3Client([
                'region'  => 'ap-southeast-1', // 例如 'us-west-2'
                'version' => 'latest',
                'credentials' => [
                    'key'    => 'AKIAQE43J644JSGMVYUX',
                    'secret' => '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',
                ],
            ]);

            $bucket = 'ysmhh';
            $Path = input('url'); // 本地文件路径

            $arr = explode('.',trim($Path));

            $key = time().'.'.end($arr); // S3 中的文件名
            $filePath = $key;
            copy($Path, $filePath);

            try {
                $result = $s3Client->putObject([
                    'Bucket' => $bucket,
                    'Key'    => $key,
                    'SourceFile' => $filePath,
                    'ACL'    => 'public-read', // 设置 ACL（可选）
                ]);
                unlink($filePath);
                return json(['status'=>true,'data'=>$result['ObjectURL']]);
            } catch (AwsException $e) {
                unlink($filePath);
                return json(['status'=>false,'data'=>$e->getMessage()]);
            }

        }

    }
    
    public function hook()
    {
     
//本地路径
        $local = '/www/wwwroot/aws';
        //仓库地址
        $remote = 'git@gitee.com:chenvle/aws.git';

        echo shell_exec("cd {$local} && git pull {$remote} 2>&1");
        die('done ' . date('Y-m-d H:i:s', time()));
    }
    
    
}
